package domain;

import dao.RootDAO;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Root implements Serializable{
    /* POJO */
    private Integer id_root;
    private String path;
    private HashSet<TipoLinkIn> insiemeLinkIn = new HashSet<TipoLinkIn>();

    /**
     * @return the id_root
     */
    public Integer getId_root() {
        return id_root;
    }

    public Root setId_root(Integer i){
        id_root = i;
        return this;
    }
    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }

    /**
     * @param path the path to set
     */
    public Root setPath(String path) {
        this.path = path;
        return this;
    }
    
    public Root inserisciLinkIn(TipoLinkIn link){
        if(link != null && link.getRoot() == this)
            ManagerIn.inserisci(link);
        return this;
    }
    
    public Root eliminaLinkIn(TipoLinkIn link){
        if(link != null && link.getRoot() == this)
            ManagerIn.elimina(link);
        return this;
    }
    
    public Set<TipoLinkIn> getLinkIn() {
        return (Set<TipoLinkIn>)insiemeLinkIn.clone();
    }
        
    public void inserisciPerManagerIn(ManagerIn aThis) {
        if(aThis != null) insiemeLinkIn.add(aThis.getLink());
    }

    public void eliminaPerManagerIn(ManagerIn aThis) {
        if(aThis != null) insiemeLinkIn.remove(aThis.getLink());
    }
    
    public Root createOnDB() throws EccezioneUpdater{
        RootDAO.create(this);
        return this;
    }
    
    public Root readFromDB() throws EccezioneUpdater{
        RootDAO.read(this);
        return this;
    }
    
    public Root updateOnDB() throws EccezioneUpdater{
        RootDAO.update(this);
        return this;
    }
    
    public Root deleteFromDB() throws EccezioneUpdater{
        RootDAO.delete(this);
        return this;
    }
    
    public Root createLinkOnDB(TipoLinkIn link) throws EccezioneUpdater, EccezionePrecondizioni{
        if(link != null && link.getRoot() == this)
            RootDAO.createLink(link);
        return this;
    }
    
    public Root deleteLinkFromDB(TipoLinkIn link) throws EccezioneUpdater, EccezionePrecondizioni{
        if(link != null && link.getRoot() == this)
            RootDAO.deleteLink(link);
        return this;
    }
    
    @Override
    public String toString(){
        return path;
    }
}